package com.lyh.dt.aspect;

import com.lyh.dt.connection.DtConnection;
import com.lyh.dt.server.TransactionClient;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;

/**
 * @author liuyuhong
 */
@Aspect
@Component
public class DtDataSourceAspect {

    @Around("execution(* javax.sql.DataSource.getConnection(..))")
    public Connection around(ProceedingJoinPoint point) throws Throwable {
        if (TransactionClient.threadLocalDt.get() != null) {
            return new DtConnection((Connection) point.proceed(), TransactionClient.threadLocalDt.get());
        } else {
            return (Connection) point.proceed();
        }
    }

}
